package com.production.CoursePlatform.mapper;

import com.production.CoursePlatform.dto.NoticeWithIsReadDTO;
import com.production.CoursePlatform.entity.Notice;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NoticeMapper extends BaseMapper<Notice> {

        // 增加数据
        @Insert("INSERT INTO course_notice (ct_id, cn_title, cn_content, cn_time, attachment) " +
                        "VALUES ( #{ctID}, #{cnTitle}, #{cnContent}, #{cnTime}, #{attachment})")
        @Options(useGeneratedKeys = true, keyProperty = "cnID")
        public int insertNotice(Notice notice);

        @Update("UPDATE course_notice SET unreadNumber = #{unreadNumber} WHERE cn_id = #{cnID}")
        int setUnreadNumber(int cnID, int unreadNumber);

        // 根据 cn_id 查找
        @Select("SELECT * FROM course_notice WHERE cn_id = #{cnID}")
        public Notice selectNoticeByCnID(@Param("cnID") int cnID);

        // 根据 cn_id 修改
        @Update("UPDATE course_notice SET ct_id = #{ctID},  cn_title = #{cnTitle}, " +
                        "cn_content = #{cnContent}, attachment = #{attachment} " +
                        "WHERE cn_id = #{cnID}")
        public int updateNotice(Notice notice);

        // 根据 cn_id 删除
        @Delete("DELETE FROM course_notice WHERE cn_id = #{cnID}")
        public int deleteNoticeByCnID(@Param("cnID") int cnID);

        // 根据 ct_id 查找
        @Select("SELECT * FROM course_notice WHERE ct_id = #{ctID}")
        public List<Notice> selectNoticesByCtID(@Param("ctID") int ctID);

        // 根据 sID 查找所有通知
        @Select("SELECT course_notice.cn_id,ct_id,snID,cn_title,cn_content,cn_time,isRead FROM course_notice" +
                        " join student_notice ON course_notice.cn_id = student_notice.cnID" +
                        " WHERE sID = #{sID}")
        List<NoticeWithIsReadDTO> findAllNoticesBySID(@Param("sID") int sID);

}
